package com.bltec.emergency.controller.recordController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.RecordDisaster;
import com.bltec.emergency.service.recordService.RecordDisasterService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import com.bltec.emergency.utils.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/disaster")
@RequiresPermissions("project:record")
public class DisasterController {
    @Resource
    private RecordDisasterService recordDisasterService;
    @Resource
    private UserService userService;
    /**
     * 获得所有用户
     */
    @RequestMapping("getAllDisaster")
    public ResponseJson getUser() {
        try {
            List<RecordDisaster> recordDisaster = recordDisasterService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(recordDisaster));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加用户
     * id,disasterName,disasterType,occuredPosition,issueTime,casualties,href
     */
    @RequestMapping("addDisaster")
    @RequiresPermissions("record:insert")
    public ResponseJson addUser(String disasterName, String disasterType, String occurredPosition, Date issueTime, String casualties, @RequestParam(value = "href",required = false) MultipartFile href) {
        try {
            String filePath= FileUtils.writeToFile(href.getBytes(), ResourceUtils.getURL("classpath:").getPath(),href.getOriginalFilename());
            RecordDisaster disaster = new RecordDisaster();
            disaster.setDisasterName(disasterName);
            disaster.setDisasterType(disasterType);
            disaster.setOccurredPosition(occurredPosition);
            disaster.setIssueTime(issueTime);
            disaster.setCasualties(casualties);
            disaster.setHref(filePath);
            disaster.setEnable(true);
            disaster.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            disaster.setCreateTime(DateUtil.dateNow());
            recordDisasterService.add(disaster);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("record:update")
    @RequestMapping("updateDisaster")
    public ResponseJson updateUser(Long id, String disasterName, String disasterType, String occurredPosition, Date issueTime, String casualties, @RequestParam(value = "href",required = false) MultipartFile href) {
        try {
            String filePath= FileUtils.writeToFile(href.getBytes(), ResourceUtils.getURL("classpath:").getPath(),href.getOriginalFilename());
            RecordDisaster disaster = recordDisasterService.getDisasterById(id);
            disaster.setDisasterName(disasterName);
            disaster.setDisasterType(disasterType);
            disaster.setOccurredPosition(occurredPosition);
            disaster.setIssueTime(issueTime);
            disaster.setCasualties(casualties);
            disaster.setHref(filePath);
            disaster.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            disaster.setUpdateTime(DateUtil.dateNow());
            recordDisasterService.updateDisasterById(disaster);
            return new ResponseJson(Constant.OK, JSON.toJSON(disaster));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL,"更新失败"+e.getMessage());
        }
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("record:delete")
    @RequestMapping("deleteDisaster")
    public ResponseJson setDisable(Long id) {
        if (null != recordDisasterService.getDisasterById(id)) {
            if (recordDisasterService.deleteDisasterById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }


}
